/*
The following code generate Table H1- H4 in the Appendix


/*
Also notice that 
prod2code==1: mineral
prod2code==2: electricity generation
prod2code==3: food products
prod2code==4: alcoholic berage
prod2code==5: cloth
prod2code==6: clothing and apparel
prod2code==7: wooden furniture
prod2code==8: paper products
prod2code==11: fuel, diesel & gas
prod2code==14: chemical products
prod2code==16: rubber products
prod2code==18: tires&conveys belts
prod2code==19: plastic products
prod2code==20: glass& ceremic products
prod2code==21: crude steel
prod2code==22: high quality steel&steel plates
prod2code==25: heavy metal products
prod2code==26: light metal products
prod2code==29: auto parts
prod2code==30: wheels, gears&mining quipment
prod2code==35: transportation equipment
prod2code==36: abatement equipment& heavy machine
prod2code==39: common cables&elc. wires
prod2code==43:measuring tech., printer, etc  
*/
*/

 
 
 

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&    Table H1   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&     Table H1     &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*----------------------------------------------------------------------
*Part Ia. emission elasticity: sector level mean and median (Table H1)
*-----------------------------------------------------------------------
 use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear


gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=. if prod_2==9

merge m:1 frdm year using  "D:\Nanjing\2019\pollution2\data and do\ownersihp_2000-2005.dta" 
keep if _merge==3
*(3) Column 5 of Table 3
drop if theta2_E_fjt_firm<0
egen ub_E=pctile(theta2_E_fjt_firm),p(97)
egen lb_E=pctile(theta2_E_fjt_firm),p(3)

replace theta2_E_fjt_firm=. if prod_2==.



*---------------------------------------------------------
*1.1 private-mean: column 1 of Table H1
*---------------------------------------------------------
bysort prod_2: sum theta2_E_fjt_firm if theta2_E_fjt_firm>lb_E&theta2_E_fjt_firm<ub_E &ownerships==3  /* mean elasticity*/

*----------------------------------------------------------------
*1.2 state-mean: column 5 of table H1
*-----------------------------------------------------------------
bysort prod_2: sum theta2_E_fjt_firm if theta2_E_fjt_firm>lb_E&theta2_E_fjt_firm<ub_E &ownerships==1|ownerships==2  /* mean elasticity*/

*-----------------------------------------------------------------
*1.3 foreign-mean: column 9 of Table H1
*-------------------------------------------------------------------
replace theta2_E_fjt_firm =. if (prod_2==36)&(ownerships==4|ownerships==5)

bysort prod_2: sum theta2_E_fjt_firm if theta2_E_fjt_firm>lb_E&theta2_E_fjt_firm<ub_E &ownerships==4|ownerships==5  /* mean elasticity*/




*----------------------------------------------------------------------
* firm-level median elasticity
***********************************************************************
bysort prod_2 ownerships: egen m_theta2_E_fjt_firm=pctile(theta2_E_fjt_firm),p(50)	

*----------------------------------------------------------
*2.1 private-median: column 2 of Table H1
*----------------------------------------------------------
bysort prod_2: sum  m_theta2_E_fjt_firm if ownerships==3

*----------------------------------------------------------
*2.2 state-median: column 6 of Table H1
*----------------------------------------------------------
bysort prod_2: sum  m_theta2_E_fjt_firm if ownerships==1|ownerships==2


*----------------------------------------------------------
*2.3 foreign-median: column 10 of Table H1
*----------------------------------------------------------
bysort prod_2: sum  m_theta2_E_fjt_firm if ownerships==4|ownerships==5









*****************************************************************************************
* Part IIc: changes in elasticity (Table 12)
*****************************************************************************************


use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9

merge m:1 frdm year using  "D:\Nanjing\2019\pollution2\data and do\ownersihp_2000-2005.dta" 
keep if _merge==3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid MC_fp indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2 ownerships: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2 ownerships: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2 ownerships: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2 ownerships: egen m_tax_emi=pctile(diff_tax_emi),p(50)

***********************************************************
* firm-level mean difference
***********************************************************

capture drop ub_elas lb_elas
egen ub_elas=pctile(diff_theta2_E_fjt_firm),p(97)
egen lb_elas=pctile(diff_theta2_E_fjt_firm),p(3)


* mean difference in elasticity
*----------------------------------------------------------
*3.1 private_differnce mean: column 3 of Table H1
*----------------------------------------------------------
bysort prod_2: sum diff_theta2_E_fjt_firm   if diff_theta2_E_fjt_firm>lb_elas&diff_theta2_E_fjt_firm<ub_elas&ownerships==3         /*mean change in elasticity*/

*----------------------------------------------------------
*3.2 state_differnce mean: column 7 of Table H1
*----------------------------------------------------------
bysort prod_2: sum diff_theta2_E_fjt_firm   if diff_theta2_E_fjt_firm>lb_elas&diff_theta2_E_fjt_firm<ub_elas&(ownerships==1|ownerships==2)        /*mean change in elasticity*/

*----------------------------------------------------------
*3.3 foreign_differnce mean: column 11 of Table H1
*----------------------------------------------------------
replace diff_theta2_E_fjt_firm=. if prod_2==22&(ownerships==4|ownerships==5)  
bysort prod_2: sum diff_theta2_E_fjt_firm   if diff_theta2_E_fjt_firm>lb_elas&diff_theta2_E_fjt_firm<ub_elas&(ownerships==4|ownerships==5)         /*mean change in elasticity*/






****************************************************
* median elasticity difference
*----------------------------------------------
*----------------------------------------------------------
* 4.1 private-median median difference (Column 4)
*----------------------------------------------------------
bysort prod_2: sum m_elasticity if ownerships==3
 
 *----------------------------------------------------------
 * 4.2 state-median median difference (column 8)
 *----------------------------------------------------------
bysort prod_2: sum m_elasticity if ownerships==1|ownerships==2
*---------------------------------------------------------- 
 * 4.3 private-median median difference (column 12)
 *----------------------------------------------------------
bysort prod_2: sum m_elasticity if ownerships==4|ownerships==5
 



**********************************************************************************************************************************




 
 
 
 
 
 
 
 
 

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&    Table H2        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&    Table H2       &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*----------------------------------------------------------------------
*Part Id. marginal costs: sector level mean and median (Table H2)
*-----------------------------------------------------------------------

use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=. if prod_2==9

merge m:1 frdm year using  "D:\Nanjing\2019\decomposition_final\data\ownersihp_2000-2005.dta" 
keep if _merge==3

bysort prod_2: egen ub_mc=pctile(MC_fp),p(97)
bysort prod_2: egen lb_mc=pctile(MC_fp),p(3)
drop if prod_2==.
*---------------------------------------------------
*1. private: Column 1 of Table H2
*---------------------------------------------------
replace MC_fp=. if MC_fp<0&prod_2==36&ownerships==3
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==3)  /* mean implied marginal cost*/
*---------------------------------------------------
*2. state-owned: Column 5 of Table H2
*---------------------------------------------------
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==1|ownerships==2)  /* mean implied marginal cost*/
*---------------------------------------------------
*3. foreign-owned: Column 9 of Table H2
*---------------------------------------------------
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==4|ownerships==5)  /* mean implied marginal cost*/




bysort prod_2 ownerships: egen m_MC_fp=pctile(MC_fp),p(50)  /*firm-year level marginal cost*/

*---------------------------------------------------
* 4. Column 2 of Table 11
*---------------------------------------------------
egen a=pctile(MC_fp) if MC_fp>0&prod_2==22&ownerships==3
replace m_MC_fp=a if prod_2==22&ownerships==3
bysort prod_2: sum m_MC_fp if ownerships==3
*---------------------------------------------------
* 5. Column 6 of Table 11  
*---------------------------------------------------
bysort prod_2: sum m_MC_fp if ownerships==1|ownerships==2
*---------------------------------------------------
* 6 Column 10 of Table 11
*---------------------------------------------------
replace m_MC_fp=. if prod_2==36&( ownerships==4|ownership==5)
bysort prod_2: sum m_MC_fp if ownerships==4|ownership==5








*****************************************************************************************
* Part IId: changes in marginal costs(Table H2)
*****************************************************************************************
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

bysort fid year: egen MC_fp=sum(c_x_new*expenditure_m_fjt_x/(theta2_fjt_x*quantity)) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
*drop  if prod_2==9
merge m:1 frdm year using  "D:\Nanjing\2019\decomposition_final\data\ownersihp_2000-2005.dta" 
keep if _merge==3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)

*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid svshare indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2 ownerships: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2 ownerships: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2 ownerships: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2 ownerships: egen m_tax_emi=pctile(diff_tax_emi),p(50)



capture drop ub_mc lb_mc
egen ub_mc=pctile(diff_MC),p(97)
egen lb_mc=pctile(diff_MC),p(3)
drop if prod_2==.
*---------------------------------------------------
*3.1 private: column 3 of Table H2
*---------------------------------------------------
bysort prod_2: sum diff_MC   if diff_MC>lb_mc&diff_MC<ub_mc  &ownerships==3     /*mean change in mc*/

*---------------------------------------------------
*3.2 state mean: column 7 of Table H2
*---------------------------------------------------
bysort prod_2: sum diff_MC   if diff_MC>lb_mc&diff_MC<ub_mc  &ownerships==1|ownerships==2     /*mean change in mc*/

*---------------------------------------------------
*3.3 foreign mean: column 11 of Table H2
*---------------------------------------------------
replace diff_MC=. if prod_2==22&(ownerships==4|ownerships==5)
bysort prod_2: sum diff_MC   if (diff_MC>lb_mc&diff_MC<ub_mc)  &(ownerships==4|ownerships==5)     /*mean change in mc*/


****************************************************
* median elasticity difference
*----------------------------------------------

drop if theta2_E_fjt_firm<0
*-----------------------------------------------------------
* 4.1 private-median median difference: column 4 of Table 11
*-----------------------------------------------------------
bysort prod_2: sum  m_MC  if ownerships==3
 
 *----------------------------------------------------------
 * 4.2 state-median median difference; column 8 of Table 11
 *----------------------------------------------------------
bysort prod_2: sum m_MC  if ownerships==1|ownerships==2
 
 *-------------------------------------------------------------
 * 4.3 foreign-median median difference: column 12 of Table 11
 *---------------------------------------------------------------
 replace m_MC=. if (prod_2==21|prod_2==22|prod_2==36)&(ownerships==4|ownerships==5)
bysort prod_2: sum m_MC  if ownerships==4|ownerships==5
 
















 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 












 
 
 
 
 
 
 
 
 
 
 










*******************************************Table H3*********************************************************8
*******************************************Table H3*********************************************************8
*******************************************Table H3*********************************************************8






*-------------------------------------------------------------------
* Part Ia. implied markup:  sector level mean and median (Table H3)
*-------------------------------------------------------------------
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=. if prod_2==9

merge m:1 frdm year using  "D:\Nanjing\2019\decomposition_final\data\ownersihp_2000-2005.dta" 
keep if _merge==3
drop if Markup_firm<0

drop if prod_2==.
*----------------------------------------------------------------------
* firm-level mean
***********************************************************************

*----------------------------------------------------------------------
*(1.1)a private-mean: column 1 of Table H3
*----------------------------------------------------------------------
bysort prod_2: sum Markup_firm if ownerships==3

*----------------------------------------------------------------------
*(1.2)b state-mean: column 5 of Table H3
*----------------------------------------------------------------------

bysort prod_2: sum Markup_firm if ownerships==1|ownerships==2
*----------------------------------------------------------------------
*(1.3)c foreign-mean: column 9 of Table H3
*----------------------------------------------------------------------
replace Markup_firm=. if (prod_2==36)&(ownerships==4|ownerships==5)

bysort prod_2: sum Markup_firm if ownerships==4|ownerships==5




*----------------------------------------------------------------------
* firm-level median
***********************************************************************
bysort prod_2 ownerships: egen m_Markup_firm=pctile(Markup_firm),p(50)
*----------------------------------------------------------------------
*(2.1)a private-median: Column 2 of Table H3
*----------------------------------------------------------------------
bysort prod_2: sum m_Markup_firm if ownerships==3

*----------------------------------------------------------------------
*(2.2)b state-median: Column 6 of Table H3
*----------------------------------------------------------------------
bysort prod_2: sum m_Markup_firm if ownerships==1|ownerships==2

*----------------------------------------------------------------------
*(2.3)c foreign-median: Column 10 of Table H3
*----------------------------------------------------------------------
bysort prod_2: sum m_Markup_firm if ownerships==4|ownerships==5




*****************************************************************************************
* Part IIa: changes in markup (Table H3)
*****************************************************************************************

use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9
merge m:1 frdm year using  "D:\Nanjing\2019\decomposition_final\data\ownersihp_2000-2005.dta" 
keep if _merge==3
keep if Markup_firm>0
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid MC_fp indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2 ownerships: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2 ownerships: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2 ownerships: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2 ownerships: egen m_tax_emi=pctile(diff_tax_emi),p(50)

***********************************************************
* 
***********************************************************

***********************************************************
* firm-level mean difference
***********************************************************

capture drop ub_tax lb_tax
bysort ownerships: egen  ub_mp=pctile(diff_markup),p(97)
bysort ownerships: egen lb_mp=pctile(diff_markup),p(3)
drop if prod_2==.
* mean  markup:
*----------------------------------------------------------------------
* 3.1 private (Column 3 of Table H3)
*----------------------------------------------------------------------
bysort prod_2: sum diff_markup   if diff_markup>lb_mp&diff_markup<ub_mp&ownerships==3

*----------------------------------------------------------------------
*2.2 state (Column 7 of Table H3)
*----------------------------------------------------------------------
bysort prod_2: sum diff_markup   if diff_markup>lb_mp&diff_markup<ub_mp&(ownerships==1|ownerships==2)


*----------------------------------------------------------------------
*3.3 foreign (Column 11 of Table H3)
*----------------------------------------------------------------------
bysort prod_2: sum diff_markup   if (ownerships==4|ownerships==5)


****************************************************
* median markup
*----------------------------------------------
*----------------------------------------------------------------------
* 4.1 private-median markup (Column 4 of Table H3)
*----------------------------------------------------------------------
bysort prod_2: sum m_markup if ownerships==3
 
 *----------------------------------------------------------------------
 * 4.2 state-median markup (Column 8 of Table H3)
 *----------------------------------------------------------------------
bysort prod_2: sum m_markup if ownerships==1|ownerships==2
 
 *----------------------------------------------------------------------
 * 4.3 private-median  markup (Column 12 of Table H3)
 *----------------------------------------------------------------------
bysort prod_2: sum m_markup if ownerships==4|ownerships==5
 
 













*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&  Table H4  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&   Table H4   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*-------------------------------------------------------------------
* Part Ia. implied emission tax:  sector level mean and median (Table H4)
*-------------------------------------------------------------------

use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/


merge m:1 frdm year using  "D:\Nanjing\2019\decomposition_final\data\ownersihp_2000-2005.dta" 
replace tax_emi=. if prod_2==9&(ownerships==1|ownerships==2|ownerships==3)
replace prod_2=43 if prod_2==9&(ownerships==4|ownerships==5)
keep if _merge==3
drop if theta2_E_fjt_firm<0
*(1) Column 1 of Table 4
 egen ub_tax=pctile(tax_emi),p(95)
 egen lb_tax=pctile(tax_emi),p(5)


*-------------------------------------------------------
*1. private: Column 1 of Table H4
*-------------------------------------------------------
bysort prod_2: sum tax_emi if tax_emi>lb_tax&tax_emi<ub_tax &(ownerships==3)  /* mean implied emission tax*/

*-------------------------------------------------------
*2. state-owned: column 5 of Table H4
*-------------------------------------------------------
bysort prod_2: sum tax_emi if tax_emi>lb_tax&tax_emi<ub_tax &(ownerships==1|ownerships==2)  /* mean implied emission tax*/


*-------------------------------------------------------
*3. foreign-owned: column 9 of Table H4
*-------------------------------------------------------
replace tax_emi=. if (prod_2==36)&(ownerships==4|ownerships==5)
bysort prod_2: sum tax_emi if tax_emi>lb_tax&tax_emi<ub_tax &(ownerships==4|ownerships==5)  /* mean implied emission tax*/



bysort prod_2 ownerships: egen m_tax_emi=pctile(tax_emi),p(50)  /*firm-year level emission tax*/

*-------------------------------------------------------
*4. private median: coumn 2 of Table H4
*-------------------------------------------------------
bysort prod_2: sum m_tax_emi if ownerships==3

*-------------------------------------------------------
* state-owned: column 6 of Table H4
*-------------------------------------------------------
bysort prod_2: sum m_tax_emi if ownerships==1|ownerships==2

*-------------------------------------------------------
* foreign: column 10 of Table H4
*-------------------------------------------------------
egen a=pctile(tax_emi) if prod_2==35&(ownerships==5|ownerships==4)&tax_emi>0
egen a2=mode(a)
replace m_tax_emi=a2  if prod_2==35&(ownerships==5|ownerships==4)
bysort prod_2: sum m_tax_emi if ownerships==4|ownership==5














*****************************************************************************************
* Part IIb: changes in emission tax (Table H4)
*****************************************************************************************

*use "D:\Nanjing\2019\decomposition_final\data\both_elasticity",clear
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9
merge m:1 frdm year using  "D:\Nanjing\2019\pollution2\data and do\ownersihp_2000-2005.dta" 
keep if _merge==3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
sort fid MC_fp indc_y
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2 ownerships: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2 ownerships: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2 ownerships: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2 ownerships: egen m_tax_emi=pctile(diff_tax_emi),p(50)

***********************************************************
* 
***********************************************************

capture drop ub_tax lb_tax
bysort ownerships: egen  ub_tax=pctile(diff_tax_emi),p(95)
bysort ownerships: egen lb_tax=pctile(diff_tax_emi),p(5)
drop if prod_2==.
* mean emission tax:
*-------------------------------------------------------
* 2.1 private: clumn 3 of Table H4
*-------------------------------------------------------
bysort prod_2: sum diff_tax_emi   if diff_tax_emi>lb_tax&diff_tax_emi<ub_tax &ownerships==3

*-------------------------------------------------------
*2.2 state: clumn 7 of Table H4
*-------------------------------------------------------
bysort prod_2: sum diff_tax_emi   if diff_tax_emi>lb_tax&diff_tax_emi<ub_tax &(ownerships==1|ownerships==2)


*-------------------------------------------------------
*2.3 foreign: clumn 11 of Table H4
*-------------------------------------------------------
replace diff_tax_emi=. if prod_2==22&(ownerships==4|ownerships==5)

bysort prod_2: sum diff_tax_emi   if diff_tax_emi>lb_tax&diff_tax_emi<ub_tax &(ownerships==4|ownerships==5)


****************************************************
* median emission tax
 

*-------------------------------------------------------
* 3.1 private-median emission tax: clumn 4 of Table 13
*-------------------------------------------------------
bysort prod_2: sum m_tax_emi if ownerships==3
 
 
*-------------------------------------------------------
 * 3.2 state-median emission tax: clumn 8 of Table 13
*-------------------------------------------------------
bysort prod_2: sum m_tax_emi if ownerships==1|ownerships==2
 
 
*-------------------------------------------------------
 * 3.3 private-median emission tax: clumn 12 of Table 13
*-------------------------------------------------------
 replace m_tax_emi=. if prod_2==35&(ownerships==4|ownerships==5)
bysort prod_2: sum m_tax_emi if ownerships==4|ownerships==5
 
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
















 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
*----------------------------------------------------------------------
*Part Id. marginal costs: sector level mean and median (Table 12)
*-----------------------------------------------------------------------
use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\both_elasticity",clear

gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9

merge m:1 frdm year using  "D:\Nanjing\2019\pollution2\data and do\ownersihp_2000-2005.dta" 
keep if _merge==3

bysort prod_2: egen ub_mc=pctile(MC_fp),p(97)
bysort prod_2: egen lb_mc=pctile(MC_fp),p(3)

*1. private
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==3)  /* mean implied marginal cost*/

*2. state-owned
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==1|ownerships==2)  /* mean implied marginal cost*/

*3. foreign-owned
bysort prod_2: sum MC_fp if MC_fp>lb_mc&MC_fp<ub_mc &(ownerships==4|ownerships==5)  /* mean implied marginal cost*/



*(2) Column 2 of Table 4
bysort prod_2 ownerships: egen m_MC_fp=pctile(MC_fp),p(50)  /*firm-year level marginal cost*/

bysort prod_2: sum m_MC_fp if ownerships==3

bysort prod_2: sum m_MC_fp if ownerships==1|ownerships==2

bysort prod_2: sum m_MC_fp if ownerships==4|ownership==5








*****************************************************************************************
* Part IId: changes in marginal costs(Table 12)
*****************************************************************************************

use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\both_elasticity",clear

bysort fid year: egen MC_fp=sum(c_x_new*expenditure_m_fjt_x/(theta2_fjt_x*quantity)) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/
replace prod_2=4 if prod_2==9
merge m:1 frdm year using  "D:\Nanjing\2019\pollution2\data and do\ownersihp_2000-2005.dta" 
keep if _merge==3
egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
duplicates drop fid indc_y, force

xtset fid indc_y
tsfill, full
bysort fid: gen MC_fp_l=l.MC_fp                             /*lagged marginal cost*/
bysort fid: gen Markup_firm_l=l.Markup_firm                 /*lagged markup*/
bysort fid: gen theta2_E_fjt_firm_l=l.theta2_E_fjt_firm     /*lagged elasticity of emission*/
bysort fid: gen tax_emi_l=l.tax_emi                        /*lagged implied emission tax*/

bysort fid: gen diff_MC=(MC_fp-MC_fp_l)/MC_fp_l
bysort fid: gen diff_markup=(Markup_firm-Markup_firm_l)/Markup_firm_l
bysort fid: gen diff_theta2_E_fjt_firm=(theta2_E_fjt_firm-theta2_E_fjt_firm_l)/theta2_E_fjt_firm_l
bysort fid: gen diff_tax_emi=(tax_emi-tax_emi_l)/tax_emi_l


* only keep observations in 2005
keep if indc_y==2
bysort prod_2 ownerships: egen m_elasticity=pctile(diff_theta2_E_fjt_firm),p(50)	
bysort prod_2 ownerships: egen m_MC=pctile(diff_MC),p(50)	
bysort prod_2 ownerships: egen m_markup=pctile(diff_markup),p(50)
bysort prod_2 ownerships: egen m_tax_emi=pctile(diff_tax_emi),p(50)

***********************************************************
* Columns 3 - 4  in Table4: changes in implied emission tax
***********************************************************



**********************************************************
* Columns 7 - 6 in Table4: changes in implied marginal cost
*********************************************************
         

capture drop ub_mc lb_mc
egen ub_mc=pctile(diff_MC),p(97)
egen lb_mc=pctile(diff_MC),p(3)
*3.1 private
bysort prod_2: sum diff_MC   if diff_MC>lb_mc&diff_MC<ub_mc  &ownerships==3     /*mean change in mc*/


*3.2 state mean
bysort prod_2: sum diff_MC   if diff_MC>lb_mc&diff_MC<ub_mc  &ownerships==1|ownerships==2     /*mean change in mc*/

*3.3 foreign mean
bysort prod_2: sum diff_MC   if (diff_MC>lb_mc&diff_MC<ub_mc)  &(ownerships==4|ownerships==5)     /*mean change in mc*/


****************************************************
* median elasticity difference
*----------------------------------------------
drop if theta2_E_fjt_firm<0
* 4.1 private-median median difference
bysort prod_2: sum  m_MC  if ownerships==3
 
 
 * 4.2 state-median median difference
bysort prod_2: sum m_MC  if ownerships==1|ownerships==2
 
 
 * 4.3 foreign-median median difference
bysort prod_2: sum m_MC  if ownerships==4|ownerships==5
 
















 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
